package com.example.mobile2021606134chhnewdemo.NewsModule;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.mobile2021606134chhnewdemo.R;

import java.util.List;

public class SearchRViewAdapter extends RecyclerView.Adapter<SearchRViewAdapter.ViewHolder> {
    List<SearchData> list;
    private Context context;
    public SearchRViewAdapter(List<SearchData> list, Context context) {
        this.list = list;
        this.context = context;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_news_pic, null);
        SearchRViewAdapter.ViewHolder viewHolder = new SearchRViewAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SearchData searchData = list.get(position);
        String title = searchData.getTitle();
        String time = searchData.getTime();
        String src = searchData.getSrc();
        String category = searchData.getCategory();
        String pic = searchData.getPic();
        String content = searchData.getContent();
        String url = searchData.getUrl();
        String WebUrl = searchData.getWeburl();
        holder.tvNewsTitle1.setText(searchData.getTitle());
        holder.tvNewsSrcTime1.setText(searchData.getSrc() + " " + searchData.getTime());
        Glide.with(context).load(pic).into(holder.imageView2);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, NewContentActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("title", title);
                bundle.putString("time", time);
                bundle.putString("src", src);
                bundle.putString("category", category);
                bundle.putString("pic", pic);
                bundle.putString("content", content);
                bundle.putString("url", url);
                bundle.putString("WebUrl", WebUrl);
                intent.putExtras(bundle);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        //        TextView tvNewsTitle, tvNewsSrcTime;
        TextView tvNewsTitle1, tvNewsSrcTime1;
        ImageView imageView2;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
//            this.tvNewsTitle = itemView.findViewById(R.id.tvNewsTitle);
//            this.tvNewsSrcTime = itemView.findViewById(R.id.tvNewsSrcTime);
            this.tvNewsTitle1 = itemView.findViewById(R.id.tvNewsTitle1);
            this.tvNewsSrcTime1 = itemView.findViewById(R.id.tvNewsSrcTime1);
            this.imageView2 = itemView.findViewById(R.id.imageView2);
        }
    }
}
